package com.hibernate.hrm.entity.dto.department;

import com.hibernate.hrm.entity.Department;
import com.hibernate.hrm.entity.User;
import com.hibernate.hrm.entity.dto.employee.UserDTO;
import lombok.Data;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class DepartmentDetailDTO {
    private Integer departmentID;
    private String departmentName;
    private String departmentCode;
    private String departmentIntroduction;
    private UserDTO departmentHead;
    private DepartmentBasicDTO parentDepartment;
    private Integer departmentLevel;
    private Boolean status;
    private String createTime;
    private String updateTime;
    private List<DepartmentBasicDTO> subDepartments;
    private Integer employeeCount;

    @Data
    public static class DepartmentBasicDTO {
        private Integer departmentID;
        private String departmentName;
        private String departmentCode;
        private Boolean status;

        public DepartmentBasicDTO(Department department) {
            this.departmentID = department.getDepartmentID();
            this.departmentName = department.getDepartmentName();
            this.departmentCode = department.getDepartmentCode();
            this.status = department.getStatus();
        }
    }

    public DepartmentDetailDTO(Department department) {
        this.departmentID = department.getDepartmentID();
        this.departmentName = department.getDepartmentName();
        this.departmentCode = department.getDepartmentCode();
        this.departmentIntroduction = department.getDepartmentIntroduction();
        this.departmentHead = department.getDepartmentHeadID() != null ?
                new UserDTO(department.getDepartmentHeadID()) : null;
        this.parentDepartment = department.getDepartmentParentID() != null ?
                new DepartmentBasicDTO(department.getDepartmentParentID()) : null;
        this.departmentLevel = department.getDepartmentLevel();
        this.status = department.getStatus();
        this.createTime = department.getCreateTime() != null ?
                department.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) : null;
        this.updateTime = department.getUpdateTime() != null ?
                department.getUpdateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) : null;
        this.subDepartments = department.getSubDepartments().stream()
                .map(DepartmentBasicDTO::new)
                .collect(Collectors.toList());
    }
}
